import METHOD from "./constant"
export default {
    // 添加购物车
    [METHOD.SHOPPINGCART_ADDTO](state, data) {
        console.log(data);
        var flag = false;
        state.detailsList.forEach((i) => {
            if (i.basicInfo.id === data.basicInfo.id) {
                flag = true;
                i.num++
            }
        });
        if (!flag) {
            data.num = 1;
            state.detailsList.push(data)
        }
        localStorage.setItem("detailsList", JSON.stringify(state.detailsList))
    },
    [METHOD.SHOPPINGCART_QLUS](state, id) {
        state.detailsList.forEach((i) => {
            if (i.basicInfo.id == id) {
                i.num++
            }
        });
        localStorage.setItem("detailsList", JSON.stringify(state.detailsList))
    },
    [METHOD.SHOPPINGCART_MINUS](state, id) {
        state.detailsList.forEach((i) => {
            if (i.basicInfo.id == id) {
                if (i.num < 1) {
                    i.num = 1;
                } else {
                    i.num--
                }
            }
        });
        localStorage.setItem("detailsList", JSON.stringify(state.detailsList))
    },
    // 删除
    [METHOD.SHOPPINGCART_REMOVE](state,id){
        state.detailsList.forEach((item,index)=>{
            if(item.basicInfo.id===id){
                state.detailsList.splice(index,1)
            }
        })
        localStorage.setItem("detailsList", JSON.stringify(state.detailsList))
    },
    
}